package com.systop.busafter.banner.controller;
import com.systop.busafter.banner.service.BannerService;
import com.systop.commer.Io;
import com.systop.commer.ResponseResult;
import com.systop.commer.pojo.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

@RestController
public class BannerController {
    @Autowired
    private BannerService bannerService;

    //修改
    @RequestMapping(value = "/updatabannerid", method = RequestMethod.POST)
    public @ResponseBody
    ResponseResult updatabannerid(HttpServletRequest request, @RequestParam(value = "banner_image", required = false) MultipartFile banner_image, @RequestParam(value = "banner_name") String banner_name,
                                  @RequestParam(value = "banner_state") String banner_state, @RequestParam(value = "banner_id") String banner_id) throws IOException {
        Banner banner1 = bannerService.BannerId(Integer.parseInt(banner_id));
        if (null==banner_image) {
            banner1.setBanner_id(Integer.parseInt(banner_id));
            banner1.setBanner_name(banner_name);
            banner1.setBanner_state(banner_state);
            int i = bannerService.updatabannerid(banner1);
            if (i == 0) {
                return ResponseResult.build(500, "修改失败");
            } else {
                return ResponseResult.build(200, "修改成功", i);
            }
        } else {
                banner1.setBanner_id(Integer.parseInt(banner_id));
                banner1.setBanner_image(Io.img(banner_image, request));
                banner1.setBanner_name(banner_name);
                banner1.setBanner_state(banner_state);
                int i = bannerService.updatabannerid(banner1);
                if (i == 0) {
                    return ResponseResult.build(500, "修改失败");
                } else {
                    return ResponseResult.build(200, "修改成功", i);
                }
        }
    }

        //添加
        @RequestMapping(value="/insertbanner",method= RequestMethod.POST)
        public @ResponseBody
        ResponseResult insertbanner(HttpServletRequest request,@RequestParam(value = "banner_image",required = false) MultipartFile banner_image,
                                    @RequestParam(value = "banner_name") String banner_name, @RequestParam(value = "banner_state") String banner_state) throws IOException {
            Banner banner2 =new Banner();
            banner2.setBanner_image(Io.img(banner_image, request));
            banner2.setBanner_name(banner_name);
            banner2.setBanner_state(banner_state);
            int i =  bannerService.insertbanner(banner2);
            if (i==0) {
            return ResponseResult.build(500, "修改失败",banner2);
            } else {
             return ResponseResult.build(200, "修改成功", banner2);
            }
        }


    //查看全部
    @RequestMapping(value="/bannerListall",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult bannerListall(){
        List<Banner> list =  bannerService.bannerListall();
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }

    //通过id查
    @RequestMapping(value="/bannerIdlist",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult bannerIdlist(@RequestParam(value = "banner_id") String banner_id){
        List<Banner> list =  bannerService.bannerIdlist(Integer.parseInt(banner_id));
        if (list == null) {
            return ResponseResult.build(500, "查看失败");
        } else {
            return ResponseResult.build(200, "查看成功", list);
        }
    }
    //删除
    @RequestMapping(value="/deletebannerId",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult deletebannerId(@RequestParam(value = "banner_id") String banner_id){
       int i =  bannerService.deletebannerId(Integer.parseInt(banner_id));
        if (i == 0) {
            return ResponseResult.build(500, "删除失败");
        } else {
            return ResponseResult.build(200, "删除成功", i);
        }
    }
    //修改状态
    @RequestMapping(value="/bannerstatetureT",method= RequestMethod.POST)
    public @ResponseBody
    ResponseResult bannerstatetureT(@RequestParam(value = "banner_id") String banner_id){
        Banner banner =  bannerService.BannerId(Integer.parseInt(banner_id));
        if (banner.getBanner_state().equals("0")){
            banner.setBanner_state("1");
        }else {
            banner.setBanner_state("0");
        }
        int i = bannerService.updatabannerid(banner);
        if (i == 0) {
            return ResponseResult.build(500, "状态修改失败");
        } else {
            return ResponseResult.build(200, "状态修改成功", i);
        }
    }

}
